﻿@{
    Layout = "~/Views/Shared/Chapter.cshtml";
}
<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">The CLR's Execution Model</h3>
    </div>
    <div class="panel-body">
        <ul>
            <li><a href="#dvContent1">Compiling Source Code into Managed Modules</a></li>
            <li><a href="#dvContent2">Combining Managed Modules into Assemblies</a></li>
            <li>Loading the Common Language Runtime</li>
            <li>
                Executing Your Assembly's Code
                <ul>
                    <li>IL and Verification</li>
                    <li>Unsafe Code</li>
                </ul>
            </li>
            <li>The Native Code Generator Tool: NGen.exe</li>
            <li>The Framework Class Library</li>
            <li>The Common Type System</li>
            <li>The Commmon Language Specification</li>
            <li>Interoperability with Unmanaged Code</li>
        </ul>
    </div>
</div>

<div class="panel panel-success">
    <div class="panel-heading">
        <h3 class="panel-title">Overview</h3>
    </div>
    <div class="panel-body">
        <ul>
            <li>.NET framework design</li>
            <li>Framework Technologies</li>
            <li>Terms</li>
            <li>
                Build your Source Code
                <ul>
                    <li>Into an Application</li>
                    <li>Set of Redistributable Components(files) that contain types(classes, structures, etc)</li>
                </ul>
            </li>
            <li>Application Execution</li>
        </ul>
    </div>
</div>

<div class="panel panel-info" id="dvContent1">
    <div class="panel-heading">
        <h3 class="panel-title">Compiling Source Code into Managed Modules</h3>
    </div>
    <div class="panel-body">
        <p>
            Done with your software specification, Which language should you choose? Different Languages have different capabilities.
            For example :
            <ul>
                <li>Unmanaged C/C++ code provides more control over memory management</li>
                <li>MS Visual Basic 6.0 allows rapid building of UI applications, use COM components and Database easily</li>
            </ul>
        </p>
        <p>
            CLR (Common Language Runtime), a runtime that is usable by various languages. All <a href="#">core features of CLR</a> are available to any language that targets it.
            At runtime, the CLR has no idea about the source code language.
        </p>
        <p>
            If all the languages compile into CLR understandable code, why do we need different languages because essentially the features provided by CLR are exactly same?
            Well, the answer is <em>Syntax</em>. Different languages target different genre of programmers. Programmers with different background.
            Programmers from a Mathematical or Financial background would prefer <em>APL</em> syntax. Now, to support different syntax, we must have different <em>Compilers</em>.
        </p>
        <p>
            Several Compilers which target CLR
            <ul>
                <li>
                    Created by Microsoft
                    <ul>
                        <li>C++/CLI, C#, Visual Basic, F#, Iron Python, Iron Ruby</li>
                        <li>Intermediate Language (IL) Assembler</li>
                    </ul>
                </li>
                <li>
                    Others<ul>

                        <li>
                            Ada, APL, Caml, COBOL, Eiffel, Forth, Fortran, Haskell, Lexico, LISP, LOGO, Lua, Mercury, ML, Mondrian,
                            Oberon, Pascal, Perl, PHP, Prolog, RPG, Scheme, Smalltalk, and Tcl/Tk
                        </li>
                    </ul>
                </li>
            </ul>
        </p>
        <p>
            <table>
                <tr>
                    <td><img src="~/Images/Source Code Compilation.png" alt="Compilation Process" style="height:300px;width:400px;" /></td>
                    <td>
                        Figure 1-1 shows the process of compilation of source code. Each compiler outputs a <em>Managed Module</em>.
                        Managed Module is a <a href="http://en.wikipedia.org/wiki/Portable_Executable">32 bit PE32 OR a 64 bit PE32+</a> file that requires the CLR to execute.
                        For Security, Managed Assemblies(Multiple Managed Modules and multiple resources) takes advantage of <a href="http://en.wikipedia.org/wiki/Data_Execution_Prevention">Data Execution Prevention(DEP)</a> and <a href="http://en.wikipedia.org/wiki/Address_space_layout_randomization">Address Space Layout Randomization</a> in windows.
                    </td>
                </tr>
            </table>
        </p>
        <p>
            <table>
                <tr>
                    <td colspan="2"><h4>Parts of a Managed Module</h4></td>
                </tr>
                <tr>
                    <td>PE32 or PE32+ header</td>
                    <td>
                        <ul>
                            <li>File format used for executables, object code, DLLs, FON font files in Windows OS</li>
                            <li>
                                Its a data structure that encapsulates the information necessary for the Windows OS loader to manage the wrapped executable code.
                                <ul>
                                    <li>dynamic library references for linking</li>
                                    <li>API export and import tables</li>
                                    <li>resource management data</li>
                                    <li><a href="http://en.wikipedia.org/wiki/Thread-local_storage">thread local storage(TLS)</a> data</li>
                                </ul>
                            </li>
                            <li>Indicates the type of file: GUI, CUI or DLL</li>
                            <li>Contains a timestamp of when the file was built</li>
                            <li>This header mostly contains information about the CPU code</li>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td>CLR header</td>
                    <td>
                        <ul>
                            <li>Contains the information(interpreted by CLR and utilities) that makes this a <em>Managed</em> Module</li>
                            <li>CLR version, some flags</li>
                            <li><code>MethodDef</code> metadata token of the managed module's entry point method(<code>Main</code> method)</li>
                            <li>
                                Location/Size of module's
                                <ul>
                                    <li>metadata</li>
                                    <li>resources</li>
                                    <li>strong name</li>
                                    <li>flags and other stuff</li>
                                </ul>
                            </li>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td>Metadata</td>
                    <td>Every managed module contains Metadata tables
                        <ul>
                            <li>Tables that describe the types and members defined in your source code</li>
                            <li>Tables that describe the types and members referenced by your source code</li>
                        </ul>
                    </td>
                </tr>
                <tr>
                    <td>IL Code</td>
                    <td>Compiler's output, At runtime CLR compiles the IL into native CPU instructions</td>
                </tr>
            </table>
        </p>
        <p>
            Native code compilers produce code targeted to a specific CPU architecture x86, x64, ARM. CLR compliant compilers produce IL code(<em>Managed Code</em>) which is then compiled into native code by CLR. Its Managed code as the CLR manages it's execution.
        </p>
        <p>
            Metadata is a superset of COM's Type libraries and Interface Definition Language (IDL) files. Metadata is directly associated with files containing IL code.
            Metadata is embeded in the same EXE/DLL, upon compilation of source code. Metadata is never out of sync with the source code executables.
            <ul>
                <li>Native compilers(CLR in this case) does not require C/C++ header files, as all the referenced type information exists in the files which contain IL for those types/members. Compilers can directly reach out to those IL files via Metadata in Managed Modules</li>
                <li>MSVS parses metadata to provide <em>Intellisense</em> </li>
                <li>CLR's <a href="#">Code Verification</a> process uses metadata to ensure that your code performs only "type-safe" operations</li>
                <li>Metadata allows an object to be serialized into a memory block, sent to another machine, deserialize to recreate object's state on the remote machine</li>
                <li>Metadata allows the <a href="#">Garbage Collector</a> to track the lifetime of objects. Garbage Collector goes through the metadata to find out members of a type and its references</li>
            </ul>
        </p>
        <p>End users must have CLR(being shipped as part of .NET framework) installed in order to execute any modules that contain managed code/data</p>
        <p>Microsoft's C++ compiler can output both managed/unmanaged modules. It also allows the developer to write both managed/unmanaged code in the source.</p>
    </div>
</div>

<div id="dvContent2" class="panel panel-info">
    <div class="panel-heading">
        <h3 class="panel-title">
           Combining Managed Modules into Assemblies
        </h3>
    </div>
    <div class="panel-body">

    </div>
</div>